package com.damon.android.interview.binder;

/**
 * Created by Damon.Han on 2018/10/11 0011.
 *
 * @author Damon
 */

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.damon.android.interview.R;
import com.damon.android.interview.aidl.GIMService;
import com.damon.android.interview.aidl.GetInformationManager;

/**
 * A placeholder fragment containing a simple view.
 */
public class BinderFragment extends Fragment implements View.OnClickListener {
    private Button button1;
    private TextView textView;
    private Activity activity;
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public BinderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static BinderFragment newInstance() {
        BinderFragment fragment = new BinderFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_binder, container, false);
        button1 = rootView.findViewById(R.id.button1);
        button1.setOnClickListener(this);
        textView = rootView.findViewById(R.id.textView);
        activity = getActivity();

        return rootView;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                if (!mBound) {
                    startService();
                    Toast.makeText(activity, "服务正在重连", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (manager == null) {
                    return;
                }
                try {
                    String name = manager.getName("");
                    String pwd = manager.getPwd("");
                    textView.setText("账户:" + name + "\n" + " 密码：" + pwd);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private GetInformationManager manager = null;
    //默认未连接服务
    private boolean mBound = false;

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            manager = GetInformationManager.Stub.asInterface(service);
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mBound = false;
        }
    };

    private void startService() {
        Intent intent = new Intent(activity, GIMService.class);
        intent.setAction("android.intent.action.GIMService");
        intent.setPackage("damon.com.aidlserver");
        activity.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!mBound) {
            startService();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mBound) {
            activity.unbindService(mServiceConnection);
            mBound = false;
        }
    }
}